<?php
 
namespace app\controllers;

use Yii; // 导入Yii
use yii\web\Controller; // 导入Controller
use app\controllers\BaseController;
use app\common\Deal;
use app\models\GameUser;



class InitController extends Controller{
	public $enableCsrfValidation = false;
    
    //登陆 熟悉通过token来获取用户的openid
    //小程序登录确认新旧用户
	public function actionLogin()
	{
        //导入Deal类来进行获取用户的openid
		$wxdeal = new Deal();
		$redis = Yii::$app->redis;
        //接收前端数据函数
        if( Yii::$app->request->isPost ){
            //你用变量储存
            $postData = Yii::$app->request->post();
            //前端发过来code session 都是为空 身份验证 每个人每个session（他是后端程序员设定身份标识） 参数名字：参数值
            //将这些信息赋予到每个变量里面
        	$code = $postData['code'];
            $session = $postData['session'];

            //判断session是否为空
        	if( $session != Null ){
                //删除缓存里的session
        		$logininfo = Yii::$app->redis->del('3rdsession'.$session);
        	}

            //获取用户的openid
        	$logininfo = $wxdeal->getLogininfo($code);
        	$session_key = $logininfo['session_key'];
            $openid = $logininfo['openid'];
            
            //检查session_key是否存在
            if( !isset($session_key) ){
                $content = "没有找到session_key,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => 'session_key',
                );
                return BaseController::FailRespone($data);
                exit(0);
            }

            //redis储存用户信息
            $redisinfo = array(
                'openid' => $openid,   //用于数据库检验、在数据库检查是否存在这个openid
                'session_key' =>  $session_key,
            );
            //设置缓存
            $session = $wxdeal->_3rd_session(32);  //返回的session
            $key = '_3rd_session:' . $session;
            $redis->setex($key, 1209600, json_encode($redisinfo));

            //用户新旧判断 有可能前后openid不一样 数据库更新
            $user = GameUser::judgeOpenId($openid);
            if( $user ){
                $data = array(
                    'login' => true, //已经登录
                    'session' => $session,
                    'openid' => $openid,
                );
                //这个是基于basecontroller写的返回函数 用于返回给前端
                return BaseController::SuccessRespone($data);
                exit(0);
            }else{
                $data = array(
                    'login' => false,//还没完成登录录入数据库
                    'session' => $session,
                    'openid' => $openid,
                );
                //这个是基于basecontroller写的返回函数 用于返回给前端
                return BaseController::SuccessRespone($data);
                exit(0);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            //这个是基于basecontroller写的返回函数 用于返回给前端
            return BaseController::FailRespone($data);   
        }
        exit(0);
    }

    //小程序确认登陆
    //通过token encryptedData iv 获取用户的详细信息
    public function actionUserinfo(){
        //导入Deal类来进行获取用户的openid
        $wxdeal = new Deal();
        //打开缓存
        $redis = Yii::$app->redis;
        //接收前端信息
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
            //前端数据赋予变量
            $session = $postData['session'];
            $encryptedData = $postData['encryptedData'];
            $iv = $postData['iv'];
            //判断是否为空
            if( empty($session) || empty($encryptedData) || empty($iv) ){
                $content = "请尝试重新登录";
                $data = array(
                    '0' => -2,
                    '1' => $content,
                );
                return BaseController::FailRespone($data); 
                exit(0);          
            }
            //用户标识id
            $key = '_3rd_session:' . $session;
            //json解码 且获取缓存
            $result = json_decode($redis->get($key),true);
            $openid = $result['openid'];
            $session_key = $result['session_key'];
            if( empty($session_key) ){
                $content = "没有找到session_key,请尝试重新登录";
                $data = array(
                    '0' => -3,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }
            //用信息进行换取用户的详细微信信息并进行储存
            $data = $wxdeal->getUserinfo( $session_key, $encryptedData, $iv );
            if ( $data != -1 && $data != -2 && $data != -3) {
                //进行数组储存
                $userdata = array(
                    'openid' => $openid,
                    'nickname' => $data['nickName'],
                    'avatarUrl' => $data['avatarUrl'],
                );
                //调用models来储存进入数据库
                $code = GameUser::judgedata_new($userdata);
                if( $code ){
                    $data = $openid;
                    return BaseController::SuccessRespone($data);
                    exit(0);
                }else if( $code == -1 ){
                    $content = "请尝试重新登录";
                    //数据库录入失败
                    $data = array(
                        '0' => -1,
                        '1' => $content,
                    );
                    return BaseController::FailRespone($data);
                    exit(0);
                }
            }else{
                $error = $data;
                $data = array(
                    '0' => 0,
                    '1' => $error,
                );
                return BaseController::FailRespone($data);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
    }
    

    //小程序确认已登陆用户头像和昵称是否一致
    public function actionCheckan(){
        $redis = Yii::$app->redis;
        if( Yii::$app->request->isPost ){
            $postData = Yii::$app->request->post();
            $session = $postData['session'];
            $avatarurl = $postData['avatarurl'];
            $nickname = $postData['nickname'];
            $key = '_3rd_session:' . $session;
            $result = json_decode($redis->get($key),true);
            $openid = $result['openid'];
            $userdata = array(
                'openid' => $openid,
                'avatarUrl' => $avatarurl,
                'nickname' => $nickname,
            );
            $code = GameUser::judgedata_new($userdata);
            if( $code ){
                $data = "更新成功";
                return BaseController::SuccessRespone($data);
                exit(0);
            }else if( $code == -1 ){
                $content = "更新失败";
                //数据库录入失败
                $data = array(
                    '0' => -1,
                    '1' => $content,
                );
                return BaseController::FailRespone($data);
                exit(0);
            }
        }else{
            $content = "接收的posts数据为空";
            $data = array(
                '0' => -2,
                '1' => $content,
            );
            return BaseController::FailRespone($data);   
        }
        exit(0);
    }

}
